﻿using CliWrap;
using CliWrap.Buffered;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using VO.Parts;

namespace Bridges
{
    /// <summary>
    /// 部件算法
    /// </summary>
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class Parts
    {
        public async Task<string> BuQuan(string data) 
        {
            BuQuanVO vo = JsonConvert.DeserializeObject<BuQuanVO>(data);

            // 读取模板生成算法文件
            string pyPath = Path.Combine(Directory.GetCurrentDirectory(), "pytemplate");
            string template = File.ReadAllText(Path.Combine(pyPath, "bq_template.py"));
            string content = Regex.Replace(template, "{{n}}", vo.N.ToString());
            content = Regex.Replace(content, "{{input1}}", JsonConvert.SerializeObject(vo.Input1));
            File.WriteAllText(Path.Combine(pyPath, "bq.py"), content);

            // 执行算法文件
            var stdOutBuffer = new StringBuilder();
            var stdErrBuffer = new StringBuilder();
            var result = await Cli.Wrap(Path.Combine(Directory.GetCurrentDirectory(), "python", "python.exe"))
                .WithArguments("bq.py")
                .WithWorkingDirectory(Path.Combine(Directory.GetCurrentDirectory(), "pytemplate"))
                .WithValidation(CommandResultValidation.None)
                .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
                .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
                .ExecuteBufferedAsync();

            JObject jo = JObject.Parse(result.StandardOutput);

            int code = Convert.ToInt16(jo["code"]);

            if (code == 0) {
                return JsonConvert.SerializeObject(new
                {
                    StatusCode = 0,
                    Succeeded = false,
                    Data = new List<double>(),
                    Errors = jo["msg"],
                    Extras = result.StandardOutput,
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }
            return JsonConvert.SerializeObject(new
            {
                StatusCode = 1,
                Succeeded = true,
                Data = jo["matrix1"],
                Errors = "",
                Extras = result.StandardOutput,
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 单个验证
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> SingleVerify(string data)
        {
            SingleVerifyVO vo = JsonConvert.DeserializeObject<SingleVerifyVO>(data);

            // 读取模板生成算法文件
            string pyPath = Path.Combine(Directory.GetCurrentDirectory(), "pytemplate");
            string template = File.ReadAllText(Path.Combine(pyPath, "dgyz_template.py"));
            string content = Regex.Replace(template, "{{n}}", vo.N.ToString());
            content = Regex.Replace(content, "{{matrix}}", JsonConvert.SerializeObject(vo.Matrix));
            File.WriteAllText(Path.Combine(pyPath, "dgyz.py"), content);

            // 执行算法文件
            var stdOutBuffer = new StringBuilder();
            var stdErrBuffer = new StringBuilder();
            var result = await Cli.Wrap(Path.Combine(Directory.GetCurrentDirectory(), "python", "python.exe"))
                .WithArguments("dgyz.py")
                .WithWorkingDirectory(Path.Combine(Directory.GetCurrentDirectory(), "pytemplate"))
                .WithValidation(CommandResultValidation.None)
                .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
                .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
                .ExecuteBufferedAsync();

            JObject jo = JObject.Parse(result.StandardOutput);

            int code = Convert.ToInt16(jo["code"]);

            if (code == 0)
            {
                return JsonConvert.SerializeObject(new
                {
                    StatusCode = 0,
                    Succeeded = false,
                    Data = JsonConvert.DeserializeObject(result.StandardOutput),
                    Errors = jo["msg"],
                    Extras = result.StandardOutput,
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }
            return JsonConvert.SerializeObject(new
            {
                StatusCode = 1,
                Succeeded = true,
                Data = JsonConvert.DeserializeObject(result.StandardOutput),
                Errors = jo["msg"],
                Extras = result.StandardOutput,
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 全部验证
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> AllVerify(string data)
        {
            AllVerifyVO vo = JsonConvert.DeserializeObject<AllVerifyVO>(data);

            // 读取模板生成算法文件
            string pyPath = Path.Combine(Directory.GetCurrentDirectory(), "pytemplate");
            string template = File.ReadAllText(Path.Combine(pyPath, "qbyz_template.py"));
            string content = Regex.Replace(template, "{{ω}}", JsonConvert.SerializeObject(vo.ω));
            content = Regex.Replace(content, "{{λ}}", JsonConvert.SerializeObject(vo.λ));
            File.WriteAllText(Path.Combine(pyPath, "qbyz.py"), content);

            // 执行算法文件
            var stdOutBuffer = new StringBuilder();
            var stdErrBuffer = new StringBuilder();
            var result = await Cli.Wrap(Path.Combine(Directory.GetCurrentDirectory(), "python", "python.exe"))
                .WithArguments("qbyz.py")
                .WithWorkingDirectory(Path.Combine(Directory.GetCurrentDirectory(), "pytemplate"))
                .WithValidation(CommandResultValidation.None)
                .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
                .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
                .ExecuteBufferedAsync();

            JObject jo = JObject.Parse(result.StandardOutput);

            int code = Convert.ToInt16(jo["code"]);

            if (code == 0)
            {
                return JsonConvert.SerializeObject(new
                {
                    StatusCode = 0,
                    Succeeded = false,
                    Data = JsonConvert.DeserializeObject(result.StandardOutput),
                    Errors = jo["msg"],
                    Extras = result.StandardOutput,
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }
            return JsonConvert.SerializeObject(new
            {
                StatusCode = 1,
                Succeeded = true,
                Data = JsonConvert.DeserializeObject(result.StandardOutput),
                Errors = jo["msg"],
                Extras = result.StandardOutput,
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 效能仿真-部件级算法
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> Xnfz(string data) {
            XnfzVO vo = JsonConvert.DeserializeObject<XnfzVO>(data);

            // 读取模板生成算法文件
            string pyPath = Path.Combine(Directory.GetCurrentDirectory(), "pytemplate");
            string template = File.ReadAllText(Path.Combine(pyPath, "xnfz_template.py"));
            string content = Regex.Replace(template, "{{w}}", JsonConvert.SerializeObject(vo.W));
            content = Regex.Replace(content, "{{T}}", JsonConvert.SerializeObject(vo.T));
            content = Regex.Replace(content, "{{p}}", JsonConvert.SerializeObject(vo.P));
            content = Regex.Replace(content, "{{N}}", JsonConvert.SerializeObject(vo.N1));
            content = Regex.Replace(content, "{{T0}}", JsonConvert.SerializeObject(vo.T0));
            content = Regex.Replace(content, "{{L}}", JsonConvert.SerializeObject(vo.L));
            content = Regex.Replace(content, "{{n}}", JsonConvert.SerializeObject(vo.N2));
            content = Regex.Replace(content, "{{r}}", JsonConvert.SerializeObject(vo.R));
            content = Regex.Replace(content, "{{K}}", JsonConvert.SerializeObject(vo.K));
            content = Regex.Replace(content, "{{type}}", JsonConvert.SerializeObject(vo.Type));
            File.WriteAllText(Path.Combine(pyPath, "xnfz.py"), content);

            // 执行算法文件
            var stdOutBuffer = new StringBuilder();
            var stdErrBuffer = new StringBuilder();
            var result = await Cli.Wrap(Path.Combine(Directory.GetCurrentDirectory(), "python", "python.exe"))
                .WithArguments("xnfz.py")
                .WithWorkingDirectory(Path.Combine(Directory.GetCurrentDirectory(), "pytemplate"))
                .WithValidation(CommandResultValidation.None)
                .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
                .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
                .ExecuteBufferedAsync();

            JObject jo = JObject.Parse(result.StandardOutput);

            int code = Convert.ToInt16(jo["code"]);

            if (code == 0)
            {
                return JsonConvert.SerializeObject(new
                {
                    StatusCode = 0,
                    Succeeded = false,
                    Data = JsonConvert.DeserializeObject(result.StandardOutput),
                    Errors = jo["msg"],
                    Extras = result.StandardOutput,
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }
            return JsonConvert.SerializeObject(new
            {
                StatusCode = 1,
                Succeeded = true,
                Data = JsonConvert.DeserializeObject(result.StandardOutput),
                Errors = jo["msg"],
                Extras = result.StandardOutput,
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 计算重要度（态势感知分析装备分析第三步饼图算法）
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> WhatDegree(string data)
        {
            WhatDegreeVO vo = JsonConvert.DeserializeObject<WhatDegreeVO>(data);

            // 读取模板生成算法文件
            string pyPath = Path.Combine(Directory.GetCurrentDirectory(), "pytemplate");
            string template = File.ReadAllText(Path.Combine(pyPath, "jszyd_template.py"));
            string content = Regex.Replace(template, "{{ω}}", JsonConvert.SerializeObject(vo.ω));
            content = Regex.Replace(content, "{{λ}}", JsonConvert.SerializeObject(vo.λ));
            content = Regex.Replace(content, "{{n}}", vo.N.ToString());
            File.WriteAllText(Path.Combine(pyPath, "jszyd.py"), content);

            // 执行算法文件
            var stdOutBuffer = new StringBuilder();
            var stdErrBuffer = new StringBuilder();
            var result = await Cli.Wrap(Path.Combine(Directory.GetCurrentDirectory(), "python", "python.exe"))
                .WithArguments("jszyd.py")
                .WithWorkingDirectory(Path.Combine(Directory.GetCurrentDirectory(), "pytemplate"))
                .WithValidation(CommandResultValidation.None)
                .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
                .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
                .ExecuteBufferedAsync();

            JObject jo = JObject.Parse(result.StandardOutput);

            int code = Convert.ToInt16(jo["code"]);

            if (code == 0)
            {
                return JsonConvert.SerializeObject(new
                {
                    StatusCode = 0,
                    Succeeded = false,
                    Data = JsonConvert.DeserializeObject(result.StandardOutput),
                    Errors = jo["msg"],
                    Extras = result.StandardOutput,
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }
            return JsonConvert.SerializeObject(new
            {
                StatusCode = 1,
                Succeeded = true,
                Data = JsonConvert.DeserializeObject(result.StandardOutput),
                Errors = jo["msg"],
                Extras = result.StandardOutput,
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 态势感知 》 部件级 》 根据第2步选择“参数是否已知”时，选择未知时，计算下一步图表
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> FaultRate(string data)
        {
            FaultRateVO vo = JsonConvert.DeserializeObject<FaultRateVO>(data);

            Debug.WriteLine($"测试：{JsonConvert.SerializeObject(100).GetType()}");

            // 读取模板生成算法文件
            string pyPath = Path.Combine(Directory.GetCurrentDirectory(), "pytemplate");
            string template = File.ReadAllText(Path.Combine(pyPath, "wzbjjtsgz_template.py"));
            string content = Regex.Replace(template, "{{tp}}", JsonConvert.SerializeObject(vo.Tp));
            content = Regex.Replace(content, "{{x}}", JsonConvert.SerializeObject(vo.X));
            content = Regex.Replace(content, "{{ct}}", JsonConvert.SerializeObject(vo.Ct));
            content = Regex.Replace(content, "{{reliability}}", JsonConvert.SerializeObject(vo.Reliability));
            File.WriteAllText(Path.Combine(pyPath, "wzbjjtsgz.py"), content);

            // 执行算法文件
            var stdOutBuffer = new StringBuilder();
            var stdErrBuffer = new StringBuilder();
            var result = await Cli.Wrap(Path.Combine(Directory.GetCurrentDirectory(), "python", "python.exe"))
                .WithArguments("wzbjjtsgz.py")
                .WithWorkingDirectory(Path.Combine(Directory.GetCurrentDirectory(), "pytemplate"))
                .WithValidation(CommandResultValidation.None)
                .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
                .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
                .ExecuteBufferedAsync();

            //Process process = new();
            //string workdir = Path.Combine(Directory.GetCurrentDirectory());
            //process.StartInfo.FileName = Path.Combine(workdir, "python", "python.exe");
            //process.StartInfo.Arguments = Path.Combine(workdir, "pytemplate", "wzbjjtsgz.py");
            //process.StartInfo.WorkingDirectory = workdir;
            //process.StartInfo.CreateNoWindow= true;
            //process.StartInfo.UseShellExecute= false;
            //process.StartInfo.RedirectStandardInput= true;
            //process.StartInfo.RedirectStandardOutput= true;
            //process.StartInfo.RedirectStandardError= true;
            //process.Start();
            //string result = process.StandardOutput.ReadToEnd();
            //process.WaitForExit();
            //process.Close();
            //process.Dispose();

            JObject jo = JObject.Parse(result.StandardOutput);

            int code = Convert.ToInt16(jo["code"]);

            if (code == 0)
            {
                return JsonConvert.SerializeObject(new
                {
                    StatusCode = 0,
                    Succeeded = false,
                    Data = JsonConvert.DeserializeObject(result.StandardOutput),
                    Errors = jo["msg"],
                    Extras = result.StandardOutput,
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }
            return JsonConvert.SerializeObject(new
            {
                StatusCode = 1,
                Succeeded = true,
                Data = JsonConvert.DeserializeObject(result.StandardOutput),
                Errors = jo["msg"],
                Extras = result.StandardOutput,
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 态势感知 》 部件级 》 根据第2步选择“参数是否已知”时，选择已知时，计算下一步图表
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> YZFaultRate(string data)
        {
            YZFaultRateVO vo = JsonConvert.DeserializeObject<YZFaultRateVO>(data);

            // 读取模板生成算法文件
            string pyPath = Path.Combine(Directory.GetCurrentDirectory(), "pytemplate");
            string template = File.ReadAllText(Path.Combine(pyPath, "yzbjjtsgz_template.py"));
            string content = Regex.Replace(template, "{{b1}}", JsonConvert.SerializeObject(vo.B1));
            content = Regex.Replace(content, "{{miu2}}", JsonConvert.SerializeObject(vo.Miu2));
            content = Regex.Replace(content, "{{sigma2}}", JsonConvert.SerializeObject(vo.Sigma2));
            content = Regex.Replace(content, "{{miu3}}", JsonConvert.SerializeObject(vo.Miu3));
            content = Regex.Replace(content, "{{sigma3}}", JsonConvert.SerializeObject(vo.Sigma3));
            content = Regex.Replace(content, "{{m}}", JsonConvert.SerializeObject(vo.M));
            content = Regex.Replace(content, "{{gamma}}", JsonConvert.SerializeObject(vo.Gamma));
            content = Regex.Replace(content, "{{t0}}", JsonConvert.SerializeObject(vo.T0));
            content = Regex.Replace(content, "{{tp}}", JsonConvert.SerializeObject(vo.Tp));
            content = Regex.Replace(content, "{{ct}}", JsonConvert.SerializeObject(vo.Ct));
            content = Regex.Replace(content, "{{x}}", JsonConvert.SerializeObject(vo.X));
            content = Regex.Replace(content, "{{yita}}", JsonConvert.SerializeObject(vo.Yita));
            content = Regex.Replace(content, "{{reliability}}", JsonConvert.SerializeObject(vo.Reliability));
            File.WriteAllText(Path.Combine(pyPath, "yzbjjtsgz.py"), content);

            // 执行算法文件
            var stdOutBuffer = new StringBuilder();
            var stdErrBuffer = new StringBuilder();
            var result = await Cli.Wrap(Path.Combine(Directory.GetCurrentDirectory(), "python", "python.exe"))
                .WithArguments("yzbjjtsgz.py")
                .WithWorkingDirectory(Path.Combine(Directory.GetCurrentDirectory(), "pytemplate"))
                .WithValidation(CommandResultValidation.None)
                .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
                .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
                .ExecuteBufferedAsync();

            JObject jo = JObject.Parse(result.StandardOutput);

            int code = Convert.ToInt16(jo["code"]);

            if (code == 0)
            {
                return JsonConvert.SerializeObject(new
                {
                    StatusCode = 0,
                    Succeeded = false,
                    Data = JsonConvert.DeserializeObject(result.StandardOutput),
                    Errors = jo["msg"],
                    Extras = result.StandardOutput,
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }
            return JsonConvert.SerializeObject(new
            {
                StatusCode = 1,
                Succeeded = true,
                Data = JsonConvert.DeserializeObject(result.StandardOutput),
                Errors = jo["msg"],
                Extras = result.StandardOutput,
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 态势感知 》 部件级 》 第3步根据运行时间计算故障率
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> Gjyxsjjsgzl(string data)
        {
            GjyxsjJsgzlVO vo = JsonConvert.DeserializeObject<GjyxsjJsgzlVO>(data);

            // 读取模板生成算法文件
            string pyPath = Path.Combine(Directory.GetCurrentDirectory(), "pytemplate");
            string template = File.ReadAllText(Path.Combine(pyPath, "gjyxsjjsgzl_template.py"));
            string content = Regex.Replace(template, "{{tp}}", JsonConvert.SerializeObject(vo.Tp));
            content = Regex.Replace(content, "{{a}}", JsonConvert.SerializeObject(vo.A));
            content = Regex.Replace(content, "{{b}}", JsonConvert.SerializeObject(vo.B));
            content = Regex.Replace(content, "{{c}}", JsonConvert.SerializeObject(vo.C));
            content = Regex.Replace(content, "{{b1}}", JsonConvert.SerializeObject(vo.B1));
            content = Regex.Replace(content, "{{miu2}}", JsonConvert.SerializeObject(vo.Miu2));
            content = Regex.Replace(content, "{{sigma2}}", JsonConvert.SerializeObject(vo.Sigma2));
            content = Regex.Replace(content, "{{miu3}}", JsonConvert.SerializeObject(vo.Miu3));
            content = Regex.Replace(content, "{{sigma3}}", JsonConvert.SerializeObject(vo.Sigma3));
            content = Regex.Replace(content, "{{m}}", JsonConvert.SerializeObject(vo.M));
            content = Regex.Replace(content, "{{gamma}}", JsonConvert.SerializeObject(vo.Gamma));
            content = Regex.Replace(content, "{{t0}}", JsonConvert.SerializeObject(vo.T0));
            content = Regex.Replace(content, "{{ct}}", JsonConvert.SerializeObject(vo.Ct));
            content = Regex.Replace(content, "{{yita}}", JsonConvert.SerializeObject(vo.Yita));
            content = Regex.Replace(content, "{{reliability}}", JsonConvert.SerializeObject(vo.Reliability));
            File.WriteAllText(Path.Combine(pyPath, "gjyxsjjsgzl.py"), content);

            // 执行算法文件
            var stdOutBuffer = new StringBuilder();
            var stdErrBuffer = new StringBuilder();
            var result = await Cli.Wrap(Path.Combine(Directory.GetCurrentDirectory(), "python", "python.exe"))
                .WithArguments("gjyxsjjsgzl.py")
                .WithWorkingDirectory(Path.Combine(Directory.GetCurrentDirectory(), "pytemplate"))
                .WithValidation(CommandResultValidation.None)
                .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
                .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
                .ExecuteBufferedAsync();

            JObject jo = JObject.Parse(result.StandardOutput);

            int code = Convert.ToInt16(jo["code"]);

            if (code == 0)
            {
                return JsonConvert.SerializeObject(new
                {
                    StatusCode = 0,
                    Succeeded = false,
                    Data = JsonConvert.DeserializeObject(result.StandardOutput),
                    Errors = jo["msg"],
                    Extras = result.StandardOutput,
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }
            return JsonConvert.SerializeObject(new
            {
                StatusCode = 1,
                Succeeded = true,
                Data = JsonConvert.DeserializeObject(result.StandardOutput),
                Errors = jo["msg"],
                Extras = result.StandardOutput,
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 态势感知 》 部件级 》 第3步根据故障率计算预计工作时间
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> Gjgzljsyjgzsj(string data)
        {
            GjgzljsyjgzsjVO vo = JsonConvert.DeserializeObject<GjgzljsyjgzsjVO>(data);

            // 读取模板生成算法文件
            string pyPath = Path.Combine(Directory.GetCurrentDirectory(), "pytemplate");
            string template = File.ReadAllText(Path.Combine(pyPath, "gjgzljsyjgzsj_template.py"));
            string content = Regex.Replace(template, "{{tp}}", JsonConvert.SerializeObject(vo.Tp));
            content = Regex.Replace(content, "{{a}}", JsonConvert.SerializeObject(vo.A));
            content = Regex.Replace(content, "{{b}}", JsonConvert.SerializeObject(vo.B));
            content = Regex.Replace(content, "{{c}}", JsonConvert.SerializeObject(vo.C));
            content = Regex.Replace(content, "{{b1}}", JsonConvert.SerializeObject(vo.B1));
            content = Regex.Replace(content, "{{miu2}}", JsonConvert.SerializeObject(vo.Miu2));
            content = Regex.Replace(content, "{{sigma2}}", JsonConvert.SerializeObject(vo.Sigma2));
            content = Regex.Replace(content, "{{miu3}}", JsonConvert.SerializeObject(vo.Miu3));
            content = Regex.Replace(content, "{{sigma3}}", JsonConvert.SerializeObject(vo.Sigma3));
            content = Regex.Replace(content, "{{m}}", JsonConvert.SerializeObject(vo.M));
            content = Regex.Replace(content, "{{gamma}}", JsonConvert.SerializeObject(vo.Gamma));
            content = Regex.Replace(content, "{{t0}}", JsonConvert.SerializeObject(vo.T0));
            content = Regex.Replace(content, "{{reliability}}", JsonConvert.SerializeObject(vo.Reliability));
            content = Regex.Replace(content, "{{yita}}", JsonConvert.SerializeObject(vo.Yita));
            File.WriteAllText(Path.Combine(pyPath, "gjgzljsyjgzsj.py"), content);

            // 执行算法文件
            var stdOutBuffer = new StringBuilder();
            var stdErrBuffer = new StringBuilder();
            var result = await Cli.Wrap(Path.Combine(Directory.GetCurrentDirectory(), "python", "python.exe"))
                .WithArguments("gjgzljsyjgzsj.py")
                .WithWorkingDirectory(Path.Combine(Directory.GetCurrentDirectory(), "pytemplate"))
                .WithValidation(CommandResultValidation.None)
                .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
                .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
                .ExecuteBufferedAsync();

            JObject jo = JObject.Parse(result.StandardOutput);

            int code = Convert.ToInt16(jo["code"]);

            if (code == 0)
            {
                return JsonConvert.SerializeObject(new
                {
                    StatusCode = 0,
                    Succeeded = false,
                    Data = JsonConvert.DeserializeObject(result.StandardOutput),
                    Errors = jo["msg"],
                    Extras = result.StandardOutput,
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }
            return JsonConvert.SerializeObject(new
            {
                StatusCode = 1,
                Succeeded = true,
                Data = JsonConvert.DeserializeObject(result.StandardOutput),
                Errors = jo["msg"],
                Extras = result.StandardOutput,
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 建制级/装备级效能仿真
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<string> Jzjzbjxnfz(string data)
        {
            JzjzbjxnfzVO vo = JsonConvert.DeserializeObject<JzjzbjxnfzVO>(data);

            // 读取模板生成算法文件
            string pyPath = Path.Combine(Directory.GetCurrentDirectory(), "pytemplate");
            string template = File.ReadAllText(Path.Combine(pyPath, "jzjzbjxnfz_template.py"));
            string content = Regex.Replace(template, "{{Jm}}", JsonConvert.SerializeObject(vo.Jm));
            content = Regex.Replace(content, "{{T}}", JsonConvert.SerializeObject(vo.T));
            content = Regex.Replace(content, "{{JmNumber}}", JsonConvert.SerializeObject(vo.JmNumber));
            content = Regex.Replace(content, "{{POS}}", JsonConvert.SerializeObject(vo.POS));
            content = Regex.Replace(content, "{{V}}", JsonConvert.SerializeObject(vo.V));
            File.WriteAllText(Path.Combine(pyPath, "jzjzbjxnfz.py"), content);

            // 执行算法文件
            var stdOutBuffer = new StringBuilder();
            var stdErrBuffer = new StringBuilder();
            var result = await Cli.Wrap(Path.Combine(Directory.GetCurrentDirectory(), "python", "python.exe"))
                .WithArguments("jzjzbjxnfz.py")
                .WithWorkingDirectory(Path.Combine(Directory.GetCurrentDirectory(), "pytemplate"))
                .WithValidation(CommandResultValidation.None)
                .WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutBuffer))
                .WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrBuffer))
                .ExecuteBufferedAsync();

            JObject jo = JObject.Parse(result.StandardOutput);

            int code = Convert.ToInt16(jo["code"]);

            if (code == 0)
            {
                return JsonConvert.SerializeObject(new
                {
                    StatusCode = 0,
                    Succeeded = false,
                    Data = JsonConvert.DeserializeObject(result.StandardOutput),
                    Errors = jo["msg"],
                    Extras = result.StandardOutput,
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }
            return JsonConvert.SerializeObject(new
            {
                StatusCode = 1,
                Succeeded = true,
                Data = JsonConvert.DeserializeObject(result.StandardOutput),
                Errors = jo["msg"],
                Extras = result.StandardOutput,
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }
    }
}
